<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限tree</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../extend/dtree/dtree.css">
    <link rel="stylesheet" href="../../extend/dtree/font/dtreefont.css">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">

    <div style="height: 800px;overflow: auto;">
        <ul id="roleTree" class="dtree" data-id="0"></ul>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" id="saveAuth">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script type="text/javascript">

    layui.extend({
        dtree: '/extend/dtree/dtree'
    }).use(['dtree','layer','jquery','form'], function(){
        let dtree = layui.dtree,
            layer = layui.layer,
            form = layui.form,
            $ = layui.jquery;

        // 初始化树
        let authTree = dtree.render({
            elem: "#roleTree",
            width: '80%',
            url: "/permission/list",  //permission/list
            initLevel: 3,
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            dataFormat: "list",  //配置data的风格为list
            method:'get',
            checkbar:true, //开启复选框
            response:{message:"msg",statusCode: 0}, //修改response中返回数据的定义

            /* done - 数据渲染完毕的回调
            * 无论是异步请求数据，还是直接赋值数据，都会触发该回调。你可以利用该回调做一些表格以外元素的渲染。
            * */
            done: function (res,$ul,first) {

                //根据角色id(role_id)，查询角色权限id(per_id)
                let roleId = sessionStorage.getItem("roleId");
                $.get("/role/selectPermissionByRoleId",{roleId},function (res) {
                    //"1,2,3,4"
                    let ids = res.data.join(",")  //使用逗号分割
                    console.log("permissionIds="+ids)   //查询数据库表role_pers表，2号存在17和18权限id，查询到permissionIds=17,18
                    //初始化权限树
                    dtree.chooseDataInit("roleTree",ids)
                })
            }
        });

        //监听提交，点击确认保存提交数据
        $("#saveAuth").click(function () {
            console.log("确认保存")
            //获取dtree中选中的值
            var param = dtree.getCheckbarNodesParam(authTree);
            let permissionIds = param.map(p => {
                return p.nodeId;
            }).join(',')

            //获取角色id(role_id)
            let roleId = sessionStorage.getItem("roleId") //读取数据信息
            sessionStorage.removeItem("roleId") //删除指定键(roleId)的数据语法

            //发送ajax异步请求，添加角色id(role_id)权限
            $.post("/role/addRolePermission",{roleId,permissionIds},function (res) {
                if(res.code==0){
                    layer.msg(res.msg,function () {
                        //关闭弹窗
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    })
                }else {
                    layer.msg(res.msg)
                }
            })
        })

    });
</script>
</body>
</html>